angular.module('app.controllers')
    .controller('AppCtrl',[
                '$rootScope', '$scope', '$window', '$state', '$ionicLoading', 'alertService', 'accountService', 'APP_CONFIG',
        function($rootScope,    $scope, $window,    $state,     $ionicLoading, alertService,   accountService,   APP_CONFIG) {

            //延时显示loading
            $rootScope.delayLoading = 0;
            $rootScope.$watch('delayLoading', function(newVal) {
                if (newVal > 0) {
                    $ionicLoading.show({
                        template: '<ion-spinner></ion-spinner>',
                        hideOnStateChange: true,
                        delay: 100
                    });
                } else {
                    $ionicLoading.hide();
                }
            });               

            //页面跳转状态
            $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {

            });
            //用户未登录
            $rootScope.$on(APP_CONFIG.eventUserNotLogin, function() {
                $state.go('access.login');
            });
            //获取用户信息
            $rootScope.$on(APP_CONFIG.eventUserLoaded, function(user) {

            });
            //http请求异常
            $rootScope.$on(APP_CONFIG.eventHttpError, function(event, error) {
                switch (error) {
                    case 401:
                        // 全局事件，方便其他view获取该事件，并给以相应的提示或处理
                        $state.go('access.login');
                        break;
                    case 400:
                    case 403:
                    case 404:
                    case 408:
                    //timeout
                    default:
                        break;
                }
            });

        }
    ]);